%{

int fileno(FILE *stream);

typedef struct {
	int status;
	char *version;
	char **names;
	char **values;
	int length;
} T;




%}
%option reentrant stack noyywrap
%option extra-type="T *"
%x NAME VALUE BODY STATUS
%%

" "
[^ ]+				{ 
						//printf("HTTP_VERSION=%s\n", yytext);
						yyextra->version = strdup(yytext);;
						BEGIN(STATUS); 
					}

<STATUS>" "
<STATUS>[0-9]{3}	{ 
						//printf("STATUS CODE=%i\n", atoi(yytext)); 
						yyextra->status = atoi(yytext);
					}
<STATUS>.
<STATUS>\n			{ BEGIN(NAME); }

<NAME>\r?\n			{ 
						//printf("terminate\n");
						printf("\nSTATUS CODE=%i\n", yyextra->status);
						printf("number headers: %i\n", yyextra->length);
						int i;
						for(i=0;i<yyextra->length;++i)
							printf("NAME=%s, VALUE=%s<end>\n", yyextra->names[i], yyextra->values[i]);
						yyterminate(); 
					}
<NAME>":"			{ BEGIN(VALUE); }
<NAME>[^:\r\n]+		{ 
						//printf("NAME=%s\n", strdup(yytext)); 
						yyextra->names[yyextra->length] = strdup(yytext); 
					}

<VALUE>[^ \r\n][^\r\n]+		{ 
						//printf("VALUE=%s\n", strdup(yytext)); 
						yyextra->values[yyextra->length] = strdup(yytext);
						yyextra->length++;
					}
					
<VALUE>\r?\n		{ BEGIN(NAME); }


%%

int main() {
	T x;
	x.length = 0;
	x.names = malloc(100*sizeof(char *));
	x.values = malloc(100*sizeof(char *));
	
	yyscan_t scanner;
//	yylex_init ( &scanner );
	yylex_init_extra(&x, &scanner );
	yylex ( scanner );
	yylex_destroy ( scanner );
	return 0;
}
